#include <fstream>
#include "spline.h"
#include <iostream>
#include "json.h"
#include "jsoncpp.cpp"
using namespace std;

int main()
{

    ofstream proA;
    proA.open("proA.txt", std::ios::out);
    	      

 int i=-1;
 double sub_length;
 vector<vector<double>> error;
 vector<double> E_max;
 vector<double> N;

  Json::Reader reader;
  Json::Value value;
  
  ifstream fin("A.json",std::ios::binary);
  if (!fin.is_open())
    {
      cout << "Can not open A.json!" << endl;
    }
  if(reader.parse(fin,value))
    {
      for (int i=0;i<5;i++)
	{
	  double nn = value["N"][i].asInt();
	  N.push_back(nn);
	}
    }
  fin.close();
 
  for (int i=0;i<5;i++)
   {
     int n=N[i];
     double aaa=2;
     sub_length=aaa/(n-1);
     vector<double> xx(n);
     vector<double> yy(n);
     xx[0]=-1;
     double kkk=1;
     yy[0]=kkk/26;;
      for (int j=1;j<n;j++)
       {
	 xx[j]=xx[j-1]+sub_length;
	 yy[j]=kkk/(1+25*xx[j]*xx[j]);
       
       }
      double sub=aaa/((n-1)*25);


      //S_23 D1
       ppForm fA1(n,xx,yy);
       double f1;
       f1=(50*kkk)/(pow(26,2));
       fA1.answer=fA1.S_23(1,f1,-f1);
       vector<double> err1(n-1);
       vector<double> plot1; 
       double max1=0;
     for (int k=0;k<n-1;k++)
       {
	 double x=(fA1.answer[k].begin+fA1.answer[k].end)/2;
	 double y=fA1.answer[k].coe[0]+fA1.answer[k].coe[1]*x+fA1.answer[k].coe[2]*x*x+fA1.answer[k].coe[3]*pow(x,3);
	 double uuu=fA1.answer[k].coe[0]+fA1.answer[k].coe[1]*xx[k]+fA1.answer[k].coe[2]*xx[k]*xx[k]+fA1.answer[k].coe[3]*pow(xx[k],3);
	 double y_real=kkk/(1+25*x*x);
	 err1[k]=abs(y_real-y);
	 //cout << uuu << " ";
	 if (err1[k]>=max1)
	   {
	     max1=err1[k];
	   }
	 for(int s=0;s<25;s++)
	   {
	     double plotx=fA1.answer[k].begin+s*sub;
	     //cout << plotx << " ";
	     double ploty=fA1.answer[k].coe[0]+fA1.answer[k].coe[1]*plotx+fA1.answer[k].coe[2]*plotx*plotx+fA1.answer[k].coe[3]*pow(plotx,3);
	     plot1.push_back(ploty);
	   }
	 if(k==n-2)
	   {
	     double plotx=fA1.answer[k].end;
	     //cout << plotx << " ";
	     double ploty=fA1.answer[k].coe[0]+fA1.answer[k].coe[1]*plotx+fA1.answer[k].coe[2]*plotx*plotx+fA1.answer[k].coe[3]*pow(plotx,3);
	     plot1.push_back(ploty);
	   }
	 
       }
     // cout << endl;
     //error.push_back(err1);
     //E_max.push_back(max);


     ppForm fA(n,xx,yy);
       double f2;
       f2=((-50*26*26+5000*26)*kkk)/(pow(26,4));
       fA.answer=fA.S_23(2,f2,f2);
       vector<double> err(n-1);
       vector<double> plot2;
       double max2=0;
     for (int k=0;k<n-1;k++)
       {
	 double x=(fA.answer[k].begin+fA.answer[k].end)/2;
	 double y=fA.answer[k].coe[0]+fA.answer[k].coe[1]*x+fA.answer[k].coe[2]*x*x+fA.answer[k].coe[3]*pow(x,3);
	 double uuu=fA.answer[k].coe[0]+fA.answer[k].coe[1]*xx[k]+fA.answer[k].coe[2]*xx[k]*xx[k]+fA.answer[k].coe[3]*pow(xx[k],3);
	 double y_real=kkk/(1+25*x*x);
	 err[k]=abs(y_real-y);
	 //cout << err[k] << " ";
	 if (err[k]>=max2)
	   {
	     max2=err[k];
	   }
	 for(int s=0;s<25;s++)
	   {
	     double plotx=fA.answer[k].begin+s*sub;
	     double ploty=fA.answer[k].coe[0]+fA.answer[k].coe[1]*plotx+fA.answer[k].coe[2]*plotx*plotx+fA.answer[k].coe[3]*pow(plotx,3);
	     plot2.push_back(ploty);
	   }
	 if (k==n-2)
	   {
	     double plotx=fA.answer[k].end;
	     double ploty=fA.answer[k].coe[0]+fA.answer[k].coe[1]*plotx+fA.answer[k].coe[2]*plotx*plotx+fA.answer[k].coe[3]*pow(plotx,3);
	     plot2.push_back(ploty);
	   }
       }
     // cout << endl;
     //error.push_back(err);




       ppForm fA3(n,xx,yy);
       fA3.answer=fA3.S_23(3,0,0);
       vector<double> err3(n-1);
       vector<double> plot3;
       double max3=0;
     for (int k=0;k<n-1;k++)
       {
	 double x=(fA3.answer[k].begin+fA3.answer[k].end)/2;
	 double y=fA3.answer[k].coe[0]+fA3.answer[k].coe[1]*x+fA3.answer[k].coe[2]*x*x+fA3.answer[k].coe[3]*pow(x,3);
	 //double uuu=fA.answer[k].coe[0]+fA.answer[k].coe[1]*xx[k]+fA.answer[k].coe[2]*xx[k]*xx[k]+fA.answer[k].coe[3]*pow(xx[k],3);
	 double y_real=kkk/(1+25*x*x);
	 err3[k]=abs(y_real-y);
	 //cout << err[k] << " ";
	 if (err3[k]>=max3)
	   {
	     max3=err3[k];
	   }

	  for(int s=0;s<25;s++)
	   {
	     double plotx=fA3.answer[k].begin+s*sub;
	     double ploty=fA3.answer[k].coe[0]+fA3.answer[k].coe[1]*plotx+fA3.answer[k].coe[2]*plotx*plotx+fA3.answer[k].coe[3]*pow(plotx,3);
	     plot3.push_back(ploty);
	   }
	  if (k==n-2)
	    {
	      double plotx=fA3.answer[k].end;
	     double ploty=fA3.answer[k].coe[0]+fA3.answer[k].coe[1]*plotx+fA3.answer[k].coe[2]*plotx*plotx+fA3.answer[k].coe[3]*pow(plotx,3);
	     plot3.push_back(ploty);
	    }
       }
     // cout << endl;
     //error.push_back(err);



     ppForm fA4(n,xx,yy);
       
       fA4.answer=fA4.S_01();
       vector<double> err4(n-1);
       vector<double> plot4;
       double max4=0;
     for (int k=0;k<n-1;k++)
       {
	 double x=(fA4.answer[k].begin+fA4.answer[k].end)/2;
	 double y=fA4.answer[k].coe[0]+fA4.answer[k].coe[1]*x;
	 //double uuu=fA.answer[k].coe[0]+fA.answer[k].coe[1]*xx[k]+fA.answer[k].coe[2]*xx[k]*xx[k]+fA.answer[k].coe[3]*pow(xx[k],3);
	 double y_real=kkk/(1+25*x*x);
	 err4[k]=abs(y_real-y);
	 //cout << err[k] << " ";
	 if (err4[k]>=max4)
	   {
	     max4=err4[k];
	   }
	 for(int s=0;s<25;s++)
	   {
	     double plotx=fA4.answer[k].begin+s*sub;
	     double ploty=fA4.answer[k].coe[0]+fA4.answer[k].coe[1]*plotx;
	     plot4.push_back(ploty);
	   }
	 if (k==n-2)
	   {
	     double plotx=fA4.answer[k].end;
	     double ploty=fA4.answer[k].coe[0]+fA4.answer[k].coe[1]*plotx;
	     plot4.push_back(ploty);
	   }
       }
     // cout << endl;
     //error.push_back(err);

     

     if (!proA.is_open())
     return 0;
     proA << "----------" << "N = " << n << "----------" << endl;
     proA << "---" << "S_23 D1样条" << "---" << "maxnorm of error : " << max1 << endl;
     for (int p=0;p<=(n-1)*25;p++)
       {
	 proA << plot1[p] << " ";
       }
     proA << endl;

     proA << "---" << "S_23 D2样条" << "---" << "maxnorm of error : " << max2 << endl;
     for (int p=0;p<=(n-1)*25;p++)
       {
	 proA << plot2[p] << " ";
       }
     proA << endl;

     proA << "---" << "S_23 自然样条" << "---" << "maxnorm of error : " << max3 << endl;
     for (int p=0;p<=(n-1)*25;p++)
       {
	 proA << plot3[p] << " ";
       }
     proA << endl;

     proA << "---" << "S_01 样条" << "---" << "maxnorm of error : " << max4 << endl;
     for (int p=0;p<=(n-1)*25;p++)
       {
	 proA << plot4[p] << " ";
       }
     proA << endl;


    
   }

 proA.close();



 
}
